@use '@material/theme/theme-color' as mdc-theme-color;
@use '@material/switch' as mdc-switch;
@use '@material/form-field' as mdc-form-field;
@use 'sass:map';
@use '../mdc-helpers/mdc-helpers';
@use '../../material/core/theming/theming';

@mixin color($config-or-theme) {
  $config: theming.get-color-config($config-or-theme);
  $primary: theming.get-color-from-palette(map.get($config, primary));
  $accent: theming.get-color-from-palette(map.get($config, accent));
  $warn: theming.get-color-from-palette(map.get($config, warn));

  // Save original values of MDC global variables. We need to save these so we can restore the
  // variables to their original values and prevent unintended side effects from using this mixin.
  $orig-baseline-theme-color: mdc-switch.$baseline-theme-color;
  $orig-toggled-off-thumb-color: mdc-switch.$toggled-off-thumb-color;
  $orig-toggled-off-track-color: mdc-switch.$toggled-off-track-color;
  $orig-disabled-thumb-color: mdc-switch.$disabled-thumb-color;
  $orig-disabled-track-color: mdc-switch.$disabled-track-color;

  @include mdc-helpers.mat-using-mdc-theme($config) {
    mdc-switch.$baseline-theme-color: primary;
    mdc-switch.$toggled-off-thumb-color: mdc-theme-color.prop-value(surface);
    mdc-switch.$toggled-off-track-color: mdc-theme-color.prop-value(on-surface);
    mdc-switch.$disabled-thumb-color: mdc-theme-color.prop-value(surface);
    mdc-switch.$disabled-track-color: mdc-theme-color.prop-value(on-surface);

    // MDC's switch doesn't support a `color` property. We add support
    // for it by adding a CSS class for accent and warn style.
    .mat-mdc-slide-toggle {
      @include mdc-form-field.core-styles($query: mdc-helpers.$mat-theme-styles-query);

      .mdc-switch__thumb-underlay::after, .mat-ripple-element {
        background: mdc-switch.$toggled-off-ripple-color;
      }

      &.mat-primary {
        @include mdc-switch.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
      }

      &.mat-accent {
        mdc-switch.$baseline-theme-color: secondary;
        @include mdc-switch.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
      }

      &.mat-warn {
        mdc-switch.$baseline-theme-color: error;
        @include mdc-switch.without-ripple($query: mdc-helpers.$mat-theme-styles-query);
      }
    }

    // The ripple color matches the palette only when it's checked.
    .mat-mdc-slide-toggle-checked {
      .mdc-switch__thumb-underlay::after, .mat-ripple-element {
        background: $primary;
      }

      &.mat-accent {
        .mdc-switch__thumb-underlay::after, .mat-ripple-element {
          background: $accent;
        }
      }

      &.mat-warn {
        .mdc-switch__thumb-underlay::after, .mat-ripple-element {
          background: $warn;
        }
      }
    }
  }

  // Restore original values of MDC global variables.
  mdc-switch.$baseline-theme-color: $orig-baseline-theme-color;
  mdc-switch.$toggled-off-thumb-color: $orig-toggled-off-thumb-color;
  mdc-switch.$toggled-off-track-color: $orig-toggled-off-track-color;
  mdc-switch.$disabled-thumb-color: $orig-disabled-thumb-color;
  mdc-switch.$disabled-track-color: $orig-disabled-track-color;
}

@mixin typography($config-or-theme) {
  $config: theming.get-typography-config($config-or-theme);
  @include mdc-helpers.mat-using-mdc-typography($config) {
    @include mdc-switch.without-ripple($query: mdc-helpers.$mat-typography-styles-query);
    @include mdc-form-field.core-styles($query: mdc-helpers.$mat-typography-styles-query);
  }
}

@mixin density($config-or-theme) {
  $density-scale: theming.get-density-config($config-or-theme);
  .mat-mdc-slide-toggle .mdc-switch {
    @include mdc-switch.density($density-scale, $query: mdc-helpers.$mat-base-styles-query);
  }
}

@mixin theme($theme-or-color-config) {
  $theme: theming.private-legacy-get-theme($theme-or-color-config);
  @include theming.private-check-duplicate-theme-styles($theme, 'mat-mdc-slide-toggle') {
    $color: theming.get-color-config($theme);
    $density: theming.get-density-config($theme);
    $typography: theming.get-typography-config($theme);

    @if $color != null {
      @include color($color);
    }
    @if $density != null {
      @include density($density);
    }
    @if $typography != null {
      @include typography($typography);
    }
  }
}

